﻿using System;
using System.Windows.Input;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows;
using ResortManager.Wizard;
using System.Windows.Documents;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
using DALHelper.Business;
using System.Text;

namespace ResortManager
{
    /// <summary>
    /// Interaction logic for wndMain.xaml
    /// </summary>
    public partial class wndMain : RibbonWindow
    {
        public bool IsReceptionist
        {
            get;
            set;
        }

        public wndMain()
        {
            InitializeComponent();
            this.Resources.MergedDictionaries
                .Add(Microsoft.Windows.Controls
                      .Ribbon.PopularApplicationSkins.Office2007Silver);

        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát chương trình?", "Exit?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
            {
                e.Cancel = true;
            }
            base.OnClosing(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            Util.CleanUp();
            ((App)App.Current).BUSManager.UserManager.Logout(((App)App.Current).CurrentUserName);
            base.OnClosed(e);
            Application.Current.Shutdown();
        }

        private void OnCloseApplication(object sender, ExecutedRoutedEventArgs e)
        {
            Close();
        }

        private void OnLookupCommand(object sender, ExecutedRoutedEventArgs e)
        {
            wndLookup w = new wndLookup();
            w.Owner = this;
            w.ShowDialog();
        }

        private void OnBookRoomCommand(object sender, ExecutedRoutedEventArgs e)
        {
            App app = (App)App.Current;
            List<TransDetail> ls = (List<TransDetail>)app.BUSManager.Transaction.QueryTransactionDetail(app.CurrentTransactionID);

            if (ls.Count <= 0)
            {
                List<IProceedureComponent> lsProcedure = new List<IProceedureComponent>();
                lsProcedure.Add(new BRConfig0());
                lsProcedure.Add(new BRConfig1());

                ProceedureDialog w = new ProceedureDialog("Đặt phòng Holybird Resort",
                    new BRIntro(), lsProcedure, new BRConfirm(), new BRProgress(), new BRResults());
                w.HeaderTitle = "Đặt phòng Holybird Resort";
                w.HeaderDescription = "Wizard hướng dẫn đặt phòng tại Holybird Resort";

                w.Owner = this;
                w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                w.ShowInTaskbar = false;

                string strUri2 = @"pack://application:,,,/ResortManager;component/images/bookroom.png";
                w.LogoImage = new BitmapImage(new Uri(strUri2));

                w.ShowDialog();
            }
            else
            {
                wndUpdateDetails w = new wndUpdateDetails();
                w.IsAdmin = false;
                w.EditingTransID = app.CurrentTransactionID;
                w.Owner = this;
                w.ShowInTaskbar = false;
                w.WindowStartupLocation = WindowStartupLocation.CenterOwner;

                if (w.ShowDialog() == false)
                {
                    OnCloseApplication(this, null);
                }
            }
        }

        private void OnUpdateTransCommand(object sender, ExecutedRoutedEventArgs e)
        {
            App app = ((App)App.Current);
            List<IProceedureComponent> lsProcedure = new List<IProceedureComponent>();

            if (app.CurrentTransactionID == App.ADMIN_TRANSACTION_ID)     // là admin
            {
                lsProcedure.Add(new UTConfig0());
            }
            lsProcedure.Add(new UTConfig1());

            ProceedureDialog w = new ProceedureDialog("Cập nhật thông tin",
                new UTIntro(), lsProcedure, new UTConfirm(), new UTProgress(), new UTResults());

            w.HeaderTitle = "Cập nhật thông tin";
            w.HeaderDescription = "Wizard hướng dẫn cập nhật thông tin các tài khoản tại Holybird Resort";
            w.Owner = this;
            w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            w.ShowInTaskbar = false;

            if (app.CurrentTransactionID != App.ADMIN_TRANSACTION_ID)
            {
                try
                {
                    w.WizardCommonData = app.BUSManager.Transaction.QueryTransaction(app.CurrentTransactionID);
                    if (w.WizardCommonData == null)
                        throw new ArgumentException("Không tìm thấy thông tin");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Bạn không thể cập nhật thông tin tại thời điểm này.\r\nVui lòng thử lại sau.\r\n\r\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }


            string strUri2 = @"pack://application:,,,/ResortManager;component/images/register.png";
            w.LogoImage = new BitmapImage(new Uri(strUri2));

            w.ShowDialog();
        }

        private void OnRegisterCommand(object sender, ExecutedRoutedEventArgs e)
        {
            List<IProceedureComponent> lsProcedure = new List<IProceedureComponent>();
            lsProcedure.Add(new RegConfig());

            ProceedureDialog w = new ProceedureDialog("Đăng ký tài khoản",
                new RegIntro(), lsProcedure, new RegConfirm(), new RegProgress(), new RegResults());

            w.HeaderTitle = "Đăng ký tài khoản";
            w.HeaderDescription = "Wizard hướng dẫn đăng ký nghỉ mát tại Holybird Resort";
            w.Owner = this;
            w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            w.ShowInTaskbar = false;
            w.WizardCommonData = new Transaction() { StartTime = DateTime.Today, EndTime = DateTime.Today};

            string strUri2 = @"pack://application:,,,/ResortManager;component/images/register.png";
            w.LogoImage = new BitmapImage(new Uri(strUri2));

            if (w.ShowDialog() == true)
            {

            }
        }

        private void OnCheckInCommand(object sender, ExecutedRoutedEventArgs e)
        {
            List<IProceedureComponent> lsProcedure = new List<IProceedureComponent>();
            lsProcedure.Add(new CIConfig());

            ProceedureDialog w = new ProceedureDialog("Nhận phòng Holybird Resort",
                new CIIntro(), lsProcedure, new CIConfirm(), new CIProgress(), new CIResults());
            w.HeaderTitle = "Nhận phòng Holybird Resort";
            w.HeaderDescription = "Wizard hướng dẫn thủ tục nhận phòng tại Holybird Resort";

            w.Owner = this;
            w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            w.ShowInTaskbar = false;

            string strUri2 = @"pack://application:,,,/ResortManager;component/images/checkin.png";
            w.LogoImage = new BitmapImage(new Uri(strUri2));

            w.ShowDialog();
        }

        private void OnCheckOutCommand(object sender, ExecutedRoutedEventArgs e)
        {
            List<IProceedureComponent> lsProcedure = new List<IProceedureComponent>();
            lsProcedure.Add(new COConfig0());
            lsProcedure.Add(new COConfig1());

            ProceedureDialog w = new ProceedureDialog("Thanh toán chi phí",
                new COIntro(), lsProcedure, new COConfirm(), new COProgress(), new COResults());
            w.HeaderTitle = "Trả phòng";
            w.HeaderDescription = "Wizard hướng dẫn thủ tục trả phòng tại Holybird Resort";

            w.Owner = this;
            w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            w.ShowInTaskbar = false;
            w.WizardCommonData = new CheckOutData();

            string strUri2 = @"pack://application:,,,/ResortManager;component/images/checkout.png";
            w.LogoImage = new BitmapImage(new Uri(strUri2));

            w.ShowDialog();
        }

        private void OnHelpCommand(object sender, ExecutedRoutedEventArgs e)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\n");
            strBuilder.Append("\t-- HolyBird Resort Manager --\r\n\r\n");
            strBuilder.Append("Developers:\r\n");
            strBuilder.Append("\tLê Đỗ Hoàng Long\t0612215\r\n");
            strBuilder.Append("\tLê Minh Quang\t0612359\r\n");
            strBuilder.Append("\tPhạm Hoài Vũ\t0612578\r\n");
            strBuilder.Append("\tPhạm Trung Vũ\t0612579\r\n\r\n");
            strBuilder.Append("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            MessageBox.Show(strBuilder.ToString(), "About us", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void OnIsReceptionist(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = IsReceptionist;
        }

        private void OnIsGuest(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = !IsReceptionist;
        }

    }
}
